#ifndef __SHAREDMEMORY_H__
#define __SHAREDMEMORY_H__

typedef struct SM_t SM;

/*  Create shared memory at the size of _size, and naming it: _memName.                                 *
 *  The name is path has to have only characters without slashes after, and has at least 1 character    *
 *  after. A file with that name will be created in /tmp directory for internal needs for producing     *
 *  handles.On success return 0 , fail -1 and prints the error to stderr. Fail can occur with failof system calls or allocations.                                    *
 *  Return value: On success return SM handle, fail return 0 and prints the error to stderr.           */
SM* SMCreate(size_t _size, const char* _memName);

/*  Attach to existing shared memory by name. Fail can occur with failof system calls or allocations.   *
 *  On success return SM handle, fail return 0 and prints the error to stderr.                         */
SM* SMAttach(const char* _memName);

/*  Lock shared memory to the current process. The locking is actually an agreed flag between the       *
*   processes. Every process trying lock it will wait until the curr process unlock it.                 *
*   On success return 0 , fail -1 and prints the error to stderr.                                      */
int SMLock(SM* );

/*  Lock shared memory to the current process. The locking is actually an agreed flag between the       *
*   processes. Process trying lock it will wait until the curr process unlock it or timeout will        *
*   expired. The timeout is in milseconds. If 0 is given as timeout then the process will wait until    *
*   unlock. On success return 0 , fail -1 and prints the error to stderr.                              */
int SMLockTimeOut(SM* _sm, unsigned long _timeout);

/*  Unlock the shred memory. The unlocation is possible only by the locking process. The locking is     *
*   reentrant. On success return 0 , fail -1 and prints the error to stderr.                           */
int SMUnlock(SM* );

/*  Get the shared memory buffer for read an write. Remember to lock the SM before writing  *
 *  to it. On success return pointer in virtual memory for the shared memory.              */
void* SMGet(SM* );

/*  Deatach from the shared memory. Delete the SM handler. If it the last process that      *
 *  deatach remove the shared memory.                                                      */
void SMDetach(SM*);

/*  Force Remove of shared memory and the locking device. Return 0 on success, on fail 1.   *
*   Fail can occur if the name not exist or system call failures. In fail prints to stderr.*/
int SMForceRemove(const char* _memName);

#endif /* __SHAREDMEMORY_H__    */
